package com.fengqilin.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.text.MessageFormat;

public class SumActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sum);

        String data = getIntent().getExtras().getString("data");

        Log.d("==========data:", data);

        CashierEntity entity = CashierEntity.json2Object(data);
        int sum = entity.sunChange();
        if (sum != 0) {
            TextView tvStatus = findViewById(R.id.tvStatus);
            tvStatus.setText(MessageFormat.format("您的账目有问题，您需要自掏腰包: {0}", Math.abs(sum) / 100f));
//            Toast.makeText(this, "您的账目有问题，您需要自掏腰包: " + (Math.abs(sum) / 100f), Toast.LENGTH_LONG).show();
            return;
        }
        TextView tvBeiYongJin = findViewById(R.id.tvBeiYongJin);
        TextView tvShangJiao = findViewById(R.id.tvShangJiao);

        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        sb1.append("合计: ").append(entity.all());
        for (int i = 0; i < entity.entities.length; i++) {
            CashierEntity.MoneyEntity entity1 = entity.entities[i];
            sb1.append(entity1.money).append(entity1.name).append(" = ").append(entity1.shangjiao * entity1.money).append("\n");
            sb2.append(entity1.money).append(entity1.name).append(" = ").append((entity1.num - entity1.shangjiao) * entity1.money).append("\n");
        }

        tvShangJiao.setText(sb1.toString());
        tvBeiYongJin.setText(sb2.toString());
    }
}